import { createApp } from 'vue'
import App from './App.vue'
import installElementPlus from './plugins/element'
import router from './router'
import store from './store'
import * as ElementPlusIconsVue from '@element-plus/icons-vue'

// 导航守卫
// 参数1 : to 目标路由对象
// 参数2 : from 来源路由对象
// 参数3 : next() 下一步
router.beforeEach((to, from, next) => {
    if (to.meta.requiresAuth) {     // 判断该路由是否需要登录权限
        let token = store.state.token;
        if (token) {              // 通过localStorage.getItem()获取当前的 token 是否存在
            next()
        }
        else {
            next({
                path: '/',
                query: {
                    redirect: to.fullPath    // 将跳转的路由path作为参数，登录成功后跳转到该路由
                }
            })
        }
    }
    else {
        next()
    }
})


const app = createApp(App)
app.use(store).use(router)
installElementPlus(app)
for (const [key, component] of Object.entries(ElementPlusIconsVue)) {
    app.component(key, component)
}
app.mount('#app')